import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../common/api_service.dart';
import '../model/pipe_comment_data.dart';

class PipeCommentController extends GetxController {
  final String _pipeId = Get.arguments['pipeId'] ?? '';
  final String uid = Get.arguments['uid'] ?? '';
  final scrollController = ScrollController();
  var commentList = <PipeCommentModel>[].obs;
  var isLoading = false.obs;
  var hasMore = false.obs;

  int _commentPageNo = 1;

  @override
  void onInit() {
    super.onInit();

    scrollController.addListener(_loadMore);
    isLoading.value = true;
    _getPipeComments();
  }

  @override
  void onClose() {
    scrollController.dispose();
    super.onClose();
  }

  void _loadMore() {
    if (scrollController.position.pixels == scrollController.position.maxScrollExtent) {
      if (!hasMore.value) {
        return;
      }
      _commentPageNo += 1;
      _getPipeComments();
    }
  }

  _getPipeComments() {
    ApiService().getPipeComments(_pipeId, _commentPageNo, (data) {
      isLoading.value = false;
      if (data is PipeCommentData) {
        hasMore.value = data.hasNext;
        commentList.addAll(data.list);
      }
    }, () {
      isLoading.value = false;
    });
  }

  getPipeCommentReplyList(String pid, int pageNo) {
    ApiService().getPipeCommentReplyList(_pipeId, pid, pageNo, (data) {
      if (data is PipeCommentData) {
        for (var item in commentList) {
          if (item.id == pid) {
            if (item.replyList == null) {
              item.replyList = data.list;
            } else {
              item.replyList!.addAll(data.list);
            }
            commentList.refresh();
            break;
          }
        }
      }
    }, () {});
  }

  cancelLikeComment(String commentId, String? pid) {
    if (pid != null) {
      for (var item in commentList) {
        if (item.id == pid) {
          var replyList = item.replyList ?? [];
          for (var reply in replyList) {
            if (reply.id == commentId) {
              reply.userSelfLikes = 0;
              reply.likes -= 1;
              commentList.refresh();
              break;
            }
          }
        }
      }
    } else {
      for (var item in commentList) {
        if (item.id == commentId) {
          item.userSelfLikes = 0;
          item.likes -= 1;
          commentList.refresh();
          break;
        }
      }
    }
  }

  addLikeComment(String commentId, String? pid) {
    if (pid != null) {
      for (var item in commentList) {
        if (item.id == pid) {
          var replyList = item.replyList ?? [];
          for (var reply in replyList) {
            if (reply.id == commentId) {
              reply.userSelfLikes = 1;
              reply.likes += 1;
              commentList.refresh();
              break;
            }
          }
        }
      }
    } else {
      for (var item in commentList) {
        if (item.id == commentId) {
          item.userSelfLikes = 1;
          item.likes += 1;
          commentList.refresh();
          break;
        }
      }
    }
  }
}
